package com.hanthink.web.controller.pc;

import java.util.List;

import com.hanthink.common.core.domain.model.LoginUser;
import com.hanthink.common.utils.ServletUtils;
import com.hanthink.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hanthink.common.annotation.Log;
import com.hanthink.common.core.controller.BaseController;
import com.hanthink.common.core.domain.AjaxResult;
import com.hanthink.common.enums.BusinessType;
import com.hanthink.system.domain.EmQualityWeighing;
import com.hanthink.system.service.IEmQualityWeighingService;
import com.hanthink.common.utils.poi.ExcelUtil;
import com.hanthink.common.core.page.TableDataInfo;

/**
 * 质量称重Controller
 * 
 * @author tzy
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/system/weighing")
public class EmQualityWeighingController extends BaseController
{
    @Autowired
    private IEmQualityWeighingService emQualityWeighingService;

    @Autowired
    private TokenService tokenService;
    /**
     * 查询质量称重列表
     */
    @PreAuthorize("@ss.hasPermi('system:weighing:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmQualityWeighing emQualityWeighing)
    {
        startPage();
        List<EmQualityWeighing> list = emQualityWeighingService.selectEmQualityWeighingList(emQualityWeighing);
        return getDataTable(list);
    }

    /**
     * 导出质量称重列表
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:export')")
    @Log(title = "质量称重", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(EmQualityWeighing emQualityWeighing)
    {
        List<EmQualityWeighing> list = emQualityWeighingService.selectEmQualityWeighingList(emQualityWeighing);
        ExcelUtil<EmQualityWeighing> util = new ExcelUtil<EmQualityWeighing>(EmQualityWeighing.class);
        return util.exportExcel(list, "weighing");
    }

    /**
     * 获取质量称重详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(emQualityWeighingService.selectEmQualityWeighingById(id));
    }


    @GetMapping("/getOne/{equipKey}")
    public AjaxResult getOne(@PathVariable("equipKey")String key){
        EmQualityWeighing emQualityWeighing = emQualityWeighingService.selectOneByEquipKey(key);
        return AjaxResult.success(emQualityWeighing);
    }

    /**
     * 新增质量称重
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:add')")
    @Log(title = "质量称重", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmQualityWeighing emQualityWeighing)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String username = loginUser.getUsername();
        emQualityWeighing.setCreatedBy(username);
        return toAjax(emQualityWeighingService.insertEmQualityWeighing(emQualityWeighing));
    }

    /**
     * 修改质量称重
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:edit')")
    @Log(title = "质量称重", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmQualityWeighing emQualityWeighing)
    {
        return toAjax(emQualityWeighingService.updateEmQualityWeighing(emQualityWeighing));
    }

    /**
     * 删除质量称重
     */
//    @PreAuthorize("@ss.hasPermi('system:weighing:remove')")
    @Log(title = "质量称重", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(emQualityWeighingService.deleteEmQualityWeighingByIds(ids));
    }
}
